﻿using EBA.IoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.Serialization
{
    /// <summary>
    /// JsonSerializer factory
    /// </summary>
    public class Serializer:ISerializer
    {
        public Serializer(string format = "JSON")
        {  
            if (Container.Default.TryGetExport<ISerializer>(format, out _Serializer) ==false)
            {
                switch (format.ToUpperInvariant())
                {
                    case "JSON":
                        this._Serializer = new JsonSerializer();
                        break;
                    //case "XML":
                    //    this._Serializer = new XmlSerializer();
                    //    break;
                    default:
                        throw new NotSupportedException(format);
                }
            }
        }

        protected ISerializer _Serializer;

        public string Serialize<T>(T instance)
        {
            return this._Serializer.Serialize<T>(instance);
        }

        public string SerializeObject(object instance)
        {
            return this._Serializer.SerializeObject(instance);
        }

        public object DeserializeObject(Type type, string json)
        {
            return this._Serializer.DeserializeObject(type, json);
        }

        public T Deserialize<T>(string json)
        {

            return this._Serializer.Deserialize<T>(json);
        }
    }
}
